package com.zegome.game.gomestory.main;

import android.graphics.Bitmap;
import android.util.Log;
import aurelienribon.tweenengine.Tween;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.facebook.Session;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.facebook.IFacebookEventListener;
import com.zegome.facebook.IFacebookProcessorListener;
import com.zegome.game.gomestory.main.scene.chapter.ChapterScreen;
import com.zegome.game.gomestory.main.scene.draw.Draw2Screen;
import com.zegome.game.gomestory.main.scene.draw.Draw3Screen;
import com.zegome.game.gomestory.main.scene.draw.DrawScreen;
import com.zegome.game.gomestory.main.scene.level.LevelScreen;
import com.zegome.game.gomestory.main.scene.menu.MenuScreen;
import com.zegome.game.gomestory.main.scene.play.PlayingScreen;
import com.zegome.game.gomestory.main.scene.vs.VS2Screen;
import com.zegome.game.gomestory.main.scene.vs.VSScreen;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.main.ZegomeActivity;
import com.zegome.game.screen.GameScreen;
import com.zegome.game.screen.GameScreenOption;
import com.zegome.game.screen.OrthographicCameraGame;
import com.zegome.game.util.helper.WidgetAccessor;
import com.zegome.payment.appota.IAppotaPaymentBridge;
import com.zegome.payment.appota.IAppotaPaymentListener;
import com.zegome.payment.appota.IAppotaPaymentProcessorListener;
import com.zegome.payment.appota.ISmsProcessor;
import com.zegome.payment.google.IGooglePaymentListener;

public class GomeController extends AndroidGameController implements IFacebookBridge, IAppotaPaymentBridge{
	
	private IFacebookEventListener mFBEventListener;
	
	private IAppotaPaymentListener mAppotaPaymentEvent;
	private IGooglePaymentListener mGooglePaymentEvent;
	
	@Override
	public void onLogin(final IFacebookProcessorListener processorListener) {
		// TODO Auto-generated method stub
		mFBEventListener.onLogin(processorListener);
	}


	@Override
	public void callBankPurchase(IAppotaPaymentProcessorListener processor) {
		// TODO Auto-generated method stub
		mAppotaPaymentEvent.callBankPurchase(processor);
	}

	@Override
	public void callPaypalPurchase(IAppotaPaymentProcessorListener processor) {
		// TODO Auto-generated method stub
		mAppotaPaymentEvent.callPaypalPurchase(processor);
	}

	@Override
	public void callCardPurchase(IAppotaPaymentProcessorListener processor) {
		// TODO Auto-generated method stub
		mAppotaPaymentEvent.callCardPurchase(processor);
	}

	@Override
	public void callSmsAuto(ISmsProcessor processor) {
		// TODO Auto-generated method stub
		mAppotaPaymentEvent.callSmsAuto(processor);
	}

	@Override
	public void callSmsPurchase(ISmsProcessor processor) {
		// TODO Auto-generated method stub
		mAppotaPaymentEvent.callSmsPurchase(processor);
	}
	
	public GomeController(ZegomeActivity activity, final IFacebookEventListener listener, final IAppotaPaymentListener appota, final IGooglePaymentListener google) {
		super(activity);
		// TODO Auto-generated constructor stub
		mFBEventListener = listener;
		mGooglePaymentEvent = google;
		mAppotaPaymentEvent = appota;
	}

	public Center ctr;
	
	
	@Override
	public void onControllerResume() {
		// TODO Auto-generated method stub
		ctr.d.isNetworkAvaiable = ctr.androidHelper.isNetworkAvailable();
		if(ctr.d.isNetworkAvaiable == false){
			ctr.d.isLogined = false;
			ctr.d.getNotifyOffline();
		}
		Session session = Session.getActiveSession();
		if (session != null && session.isOpened()) {
			ctr.d.isLoginedFB = true;
		} else {
			ctr.d.isLoginedFB = false;
		}
		if(ctr.d.isNetworkAvaiable){
			ctr.androidHelper.admobControl.refresh();
		}
		
		super.onControllerResume();
	}

	@Override
	public void dispose() {
		ctr.g.dispose();
		super.dispose();
	}

	@Override
	public GameScreenOption onCreate() {
		// TODO Auto-generated method stub
				
		this.setFixFPS(true);
		
		ctr.g.mSkin = new Skin(Gdx.files.internal(ctr.g.ASSET_FOLDER_DATA +ctr.g.ASSET_SKIN));
		
		ctr.g.CAMERA_WIDTH = 800;
		ctr.g.CAMERA_HEIGHT = 480;		
		
								
		ctr.a.reset();
		ctr.d.init();
							
		ctr.androidHelper.init();		
		ctr.d.getAccount();
		ctr.d.getAvatarInfo();		
		
		ctr.d.isNetworkAvaiable = ctr.androidHelper.isNetworkAvailable();
		
		ctr.d.isLDPI = Gdx.graphics.getWidth()<=480?true:false;
		
		ctr.lang.init();
		
		if(ctr.d.isNetworkAvaiable == false){
			ctr.d.getNotifyOffline();
		}
		
		final OrthographicCameraGame camera = new OrthographicCameraGame(
				ctr.g.CAMERA_WIDTH, ctr.g.CAMERA_HEIGHT);		
		camera.position.set(ctr.g.CAMERA_WIDTH / 2, ctr.g.CAMERA_HEIGHT / 2, 0);
		camera.update();		
		final GameScreenOption option = new GameScreenOption(camera, 1);
				
		//Gdx.input.setCatchBackKey(true);
		return option;
	}
	
	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub		
		ctr.a.loadCommond2();
		ctr.a.loadMenu();
		ctr.a.loadMusic1();
		ctr.a.loadMusic2();
		ctr.a.loadGame();
		ctr.a.loadSplash();
	}

	@Override
	public GameScreen onLoadScreen() {
		// TODO Auto-generated method stub
		final MenuScreen menuScreen = new MenuScreen(this,"MenuScreen",this);
		menuScreen.setCtr(ctr);
		final LevelScreen levelScreen = new LevelScreen(this,"LevelScreen",this);
		levelScreen.setCtr(ctr);
		final PlayingScreen playingScreen = new PlayingScreen(this,"GameScreen",this);
		playingScreen.setCtr(ctr);
		final ChapterScreen chapterScreen = new ChapterScreen(this,"ChapterScreen",this);
		chapterScreen.setCtr(ctr);
		final VSScreen vsScreen = new VSScreen(this,"VSScreen",this);		
		vsScreen.setCtr(ctr);
		final VS2Screen vs2Screen = new VS2Screen(this,"VS2Screen",this);	
		vs2Screen.setCtr(ctr);
		final DrawScreen drawScreen = new DrawScreen(this,"DrawScreen",this);	
		drawScreen.setCtr(ctr);
		final Draw2Screen draw2Screen = new Draw2Screen(this,"Draw2Screen",this);	
		draw2Screen.setCtr(ctr);
		final Draw3Screen draw3Screen = new Draw3Screen(this,"Draw3Screen",this);	
		draw3Screen.setCtr(ctr);
		attachScreen(menuScreen,vsScreen,vs2Screen, levelScreen, playingScreen, chapterScreen,drawScreen,draw2Screen,draw3Screen);		

		return menuScreen;
	}

	@Override
	public void onComplete() {
		// TODO Auto-generated method stub		
	}


	@Override
	public void onNewsFeed(IFacebookProcessorListener processorListener, String link, String name, String caption, String descript, String pictureUrl) {
		// TODO Auto-generated method stub
		mFBEventListener.onNewsFeed(processorListener, link, name, caption, descript, pictureUrl);
	}

	@Override
	public void onOpenAction(final IFacebookProcessorListener processorListener, final Bitmap image) {
		// TODO Auto-generated method stub
		mFBEventListener.onOpenAction(processorListener, image);
	}

	@Override
	public void onSendRequest(final IFacebookProcessorListener processorListener, final String link, final String message, final String data, final boolean isFilter) {
		// TODO Auto-generated method stub
		mFBEventListener.onSendRequest(processorListener, link, message, data, isFilter);
	}
}
